package com.vimi8.chick.plugins.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.cjt2325.cameralibrary.JCameraView
import com.cjt2325.cameralibrary.listener.ErrorListener
import com.cjt2325.cameralibrary.listener.JCameraListener
import com.vimi8.chick.plugins.R
import com.vimi8.chick.plugins.constant.Constant
import com.vimi8.chick.plugins.utils.FileUtil
import kotlinx.android.synthetic.main.activity_video_record.*

class VideoRecordActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video_record)
        setFullScreen()
        initCamera()
    }

    // Activity全屏设置
    private fun setFullScreen() {
        if (Build.VERSION.SDK_INT >= 19) {
            val decorView = window.decorView
            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        } else {
            val decorView = window.decorView
            val option = View.SYSTEM_UI_FLAG_FULLSCREEN
            decorView.systemUiVisibility = option
        }
    }

    private fun initCamera() {
        val second = intent.getIntExtra("second", 15)
        val type = intent.getStringExtra("type")
        val tip = intent.getStringExtra("tip")
        val quality = intent.getIntExtra("quality", 8)

        // 没有动态设置时长的API，只能在布局文件中设置
        // jcv.setDuration(second)

        // 设置提示文字
        if (tip != null && !TextUtils.isEmpty(tip)) {
            jcv.setTip(tip)
        }
        // 设置视频保存路径
        jcv.setSaveVideoPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).absolutePath)
        // 设置只能录像或只能拍照或两种都可以（默认两种都可以）
        jcv.setFeatures(when (type) {
            "capture" -> JCameraView.BUTTON_STATE_ONLY_CAPTURE
            "recorder" -> JCameraView.BUTTON_STATE_ONLY_RECORDER
            else -> JCameraView.BUTTON_STATE_BOTH
        })
        // 设置视频质量
        jcv.setMediaQuality((if (quality > 20) 20 else quality) * 100000)
        // JCameraView监听
        jcv.setErrorLisenter(object : ErrorListener {
            override fun AudioPermissionError() {
                Toast.makeText(this@VideoRecordActivity, "没有录取权限", Toast.LENGTH_SHORT).show()
            }

            override fun onError() {
                Toast.makeText(this@VideoRecordActivity, "打开相机失败", Toast.LENGTH_SHORT).show()
            }
        })
        jcv.setJCameraLisenter(object : JCameraListener {
            override fun recordSuccess(url: String?, firstFrame: Bitmap?) {
                val intent = Intent()
                intent.putExtra(Constant.PictureConstants.VIDEO_RECORD_TYPE, Constant.PictureConstants.VIDEO_RECORD_TYPE_VIDEO)
                intent.putExtra(Constant.PictureConstants.VIDEO_RECORD_PATH, url)
                intent.putExtra(Constant.PictureConstants.VIDEO_RECORD_IMAGE, FileUtil.saveBitmap(this@VideoRecordActivity, firstFrame))
                setResult(Activity.RESULT_OK, intent)
                this@VideoRecordActivity.finish()
            }

            override fun captureSuccess(bitmap: Bitmap?) {
                val intent = Intent()
                intent.putExtra(Constant.PictureConstants.VIDEO_RECORD_TYPE, Constant.PictureConstants.VIDEO_RECORD_TYPE_IMAGE)
                intent.putExtra(Constant.PictureConstants.VIDEO_RECORD_IMAGE, FileUtil.saveBitmap(this@VideoRecordActivity, bitmap))
                setResult(Activity.RESULT_OK, intent)
                this@VideoRecordActivity.finish()
            }
        })
        // 左边按钮点击事件
        jcv.setLeftClickListener { this@VideoRecordActivity.finish() }
        // 右边按钮点击事件
        jcv.setRightClickListener {
            // Toast.makeText(this@VideoRecordActivity, "Right", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onResume() {
        super.onResume()
        jcv.onResume()
    }

    override fun onPause() {
        super.onPause()
        jcv.onPause()
    }
}
